﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.IO;

namespace QLThuVien
{
    public partial class frMuonSach : Form
    {
        public frMuonSach()
        {
            InitializeComponent();
            txtmasach.Enabled = false;
            btmuon.Enabled = false;
        }

        bool ktdocgia = false;
        bool ktsach = false;

        int sltoida, soluongmuon = 0;
        string nv = "";
        string ngaymuon = "", ngayhethantra = "";
        public void laynhanvien()
        {
            StreamReader sr = new StreamReader("luu nhan vien.txt");
            nv = sr.ReadLine();
            sr.Close();
        }

        public void muontoida()
        {
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc3 = new SqlCommand();
            sc3.CommandText = "select *FROM DOCGIA,LOAIDOCGIA WHERE DOCGIA.MADOCGIA='" + txtmadg.Text + "' AND DOCGIA.MALOAIDOCGIA=LOAIDOCGIA.MALOAIDOCGIA";
            SqlDataReader dr = txdl.lay_reader(sc3.CommandText);
            while (dr.Read())
            {
                sltoida = Convert.ToInt16(dr["SOLUONGMUONTOIDA"]);
            }
        }

        public void clear_textbandoc()
        {
            txthoten.Text = "";
            txtdiachi.Text = "";
            txtgioitinh.Text = "";
            txtDT.Text = "";
            txtngaysinh.Text = "";
            txtphieumuon.Text = "";
        }

        public void clear_textsach()
        {
            txttensach.Text = "";
            txtnamxb.Text = "";
            txtncc.Text = "";
            txtgia.Text = "";
            txttacgia.Text = "";
            txtngonngu.Text = "";
            txtnhaxb.Text = "";
            txtlinhvuc.Text = "";
            txtlanxuatban.Text = "";
            txtkho.Text = "";
            txtngan.Text = "";
            txtke.Text = "";
        }

        public void layphieutam()
        {
            phieutam = "";
            Truyxuatdulieu txdl2 = new Truyxuatdulieu();
            SqlCommand sc2 = new SqlCommand("select MAPHIEU from phieumuon");
            SqlDataReader dr2 = txdl2.lay_reader(sc2.CommandText);
            while (dr2.Read())
                phieutam = dr2[0].ToString();
            KetNoi.closeconnect();
            if (phieutam == "") { phieutam = "PM0001"; }
            else
            {
                string s = phieutam.Substring(2, phieutam.Length - 2);
                double i = double.Parse(s);
                i++;
                if (i < 10)
                {
                    s = "PM000" + i.ToString();
                }
                else if (i < 100)
                {
                    s = "PM00" + i.ToString();
                }
                else if (i < 1000)
                {
                    s = "PM0" + i.ToString();
                }
                else if (i < 10000)
                {
                    s = "PM" + i.ToString();
                }
                phieutam = s;
            }
            txtphieumuon.Text = phieutam;
            txtmasach.Enabled = true;
        }
        string ngayhethan, phieutam;
        int monthhh, dayhh, yearhh, month, year, day;

        private void bttkbandoc_Click(object sender, EventArgs e)
        {
            load_listview();
            clear_textbandoc();
            btmuon.Enabled = false;
            KetNoi.setconnect();
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT *FROM DOCGIA,LOAIDOCGIA WHERE DOCGIA.MADOCGIA='" + txtmadg.Text + "' AND DOCGIA.MALOAIDOCGIA=LOAIDOCGIA.MALOAIDOCGIA";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                txthoten.Text = Convert.ToString(dr["HOTEN"]);
                txtdiachi.Text = Convert.ToString(dr["DIACHI"]);
                txtgioitinh.Text = Convert.ToString(dr["GIOITINH"]);
                txtDT.Text = Convert.ToString(dr["TENLOAIDG"]);
                txtngaysinh.Text = Convert.ToString(dr["NGAYSINH"]);
                ktdocgia = true;
            }


            if (txthoten.Text == "")
            {
                MessageBox.Show("Không tồn tại", "Thông Báo");
            }
            else
            {
                month = Convert.ToInt16(DateTime.Now.Month.ToString());
                day = Convert.ToInt16(DateTime.Now.Day.ToString());
                year = Convert.ToInt16(DateTime.Now.Year.ToString());
                Truyxuatdulieu txdl1 = new Truyxuatdulieu();
                SqlCommand sc1 = new SqlCommand();
                sc.CommandText = "select * from The where The.MADOCGIA='" + txtmadg.Text + "'";
                SqlDataReader dr1 = txdl.lay_reader(sc.CommandText);
                while (dr1.Read())
                {
                    ngayhethan = Convert.ToString(dr1["ngayhethan"]);
                    txtngayhh.Text = ngayhethan;
                }

                if (ngayhethan.Substring(1, 1) == "/")
                {
                    monthhh = int.Parse(ngayhethan.Substring(0, 1));
                    if (ngayhethan.Substring(3, 1) == "/")
                    {
                        dayhh = int.Parse(ngayhethan.Substring(2, 1));
                        yearhh = Convert.ToInt16(ngayhethan.Substring(4, 4));
                    }
                    else if (ngayhethan.Substring(4, 1) == "/")
                    {
                        dayhh = int.Parse(ngayhethan.Substring(2, 2));
                        yearhh = int.Parse(ngayhethan.Substring(5, 4));
                    }
                }
                else
                {
                    monthhh = int.Parse(ngayhethan.Substring(0, 2));
                    if (ngayhethan.Substring(4, 1) == "/")
                    {
                        dayhh = int.Parse(ngayhethan.Substring(3, 1));
                        yearhh = Convert.ToInt16(ngayhethan.Substring(5, 4));
                    }
                    else if (ngayhethan.Substring(5, 1) == "/")
                    {
                        dayhh = int.Parse(ngayhethan.Substring(3, 2));
                        yearhh = int.Parse(ngayhethan.Substring(6, 4));
                    }
                }
                if (year > yearhh)
                {
                    MessageBox.Show("Thẻ đã hết hạn sử dụng", "Thông Báo");
                    txtmasach.Enabled = false;
                    txtphieumuon.Text = "";
                    btmuon.Enabled = false;
                }
                else if (year == yearhh)
                {
                    if (month > monthhh)
                    {
                        MessageBox.Show("Thẻ đã hết hạn sử dụng", "Thông Báo");
                        txtmasach.Enabled = false;
                        txtphieumuon.Text = "";
                        btmuon.Enabled = false;
                    }
                    else if (month == monthhh)
                    {
                        if (day > dayhh)
                        {
                            MessageBox.Show("Thẻ đã hết hạn sử dụng", "Thông Báo");
                            txtmasach.Enabled = false;
                            txtphieumuon.Text = "";
                            btmuon.Enabled = false;
                        }
                    }
                    else
                    {
                        monthhh = monthhh + 3;
                        if (dayhh > 30)
                        {
                            monthhh = monthhh + 1;
                            dayhh = dayhh - 30;
                        }
                        if (monthhh > 12 || monthhh + 3 > 12)
                        {
                            monthhh = monthhh - 12;
                            year += 1;
                        }
                        if (year > yearhh)
                        {
                            MessageBox.Show("Thẻ sắp hết hạn sử dụng", "Thông Báo");
                            layphieutam();
                        }
                        else if (year == yearhh)
                        {
                            if (month > monthhh)
                            {
                                MessageBox.Show("Thẻ sắp hết hạn sử dụng", "Thông Báo");
                                layphieutam();
                            }
                            else if (month == monthhh)
                            {
                                if (day > dayhh)
                                {
                                    MessageBox.Show("Thẻ sắp hết hạn sử dụng", "Thông Báo");
                                    layphieutam();
                                }
                            }
                        }
                    }
                }
                else
                {
                    layphieutam();
                }
            }
            bttkbandoc.Enabled = false;
            txtmadg.Enabled = false;
            KetNoi.closeconnect();
        }

        private void btsach_Click(object sender, EventArgs e)
        {
            clear_textsach();
            btmuon.Enabled = true;
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "select *from SACH,LOAISACH,NGONNGU,NXB,TACGIA,SANGTAC,NHACUNGCAP,VITRI where SACH.MASACH='" + txtmasach.Text + "' AND SACH.MANGONNGU=NGONNGU.MANGONNGU AND SACH.MALOAISACH=LOAISACH.MALOAISACH AND SACH.MANXB=NXB.MANXB AND SACH.MANCC=NHACUNGCAP.MANCC AND SACH.MAVT = VITRI.MAVT AND SACH.MASACH=SANGTAC.MASACH AND TACGIA.MATACGIA=SANGTAC.MATACGIA";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                txttensach.Text = Convert.ToString(dr["TENSACH"]);
                txtnamxb.Text = Convert.ToString(dr["NAMXUATBAN"]);
                txtgia.Text = Convert.ToString(dr["GIA"]);
                txtncc.Text = Convert.ToString(dr["TENNCC"]);
                txtkho.Text = Convert.ToString(dr["KHO"]);
                txtngan.Text = Convert.ToString(dr["NGAN"]);
                txtke.Text = Convert.ToString(dr["ke"]);
                txttacgia.Text = Convert.ToString(dr["HOTEN"]);
                txtngonngu.Text = Convert.ToString(dr["NGONNGU"]);
                txtnhaxb.Text = Convert.ToString(dr["TENNXB"]);
                txtlinhvuc.Text = Convert.ToString(dr["TENLOAISACH"]);
                txtlanxuatban.Text = Convert.ToString(dr["LANXUATBAN"]);
                ktsach = true;
            }
            if (txttensach.Text == "")
            {
                MessageBox.Show("Không Tồn tại", "Thông Báo");
            }
            else
            {
                KetNoi.closeconnect();
                KetNoi.setconnect();
                bool sach = false;
                Truyxuatdulieu txdl1 = new Truyxuatdulieu();
                SqlCommand sc1 = new SqlCommand();
                string masach = "";
                sc1.CommandText = "SELECT *FROM SACH,PHIEUMUON,SACH_PHIEU WHERE PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH=SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL";
                SqlDataReader dr1 = txdl.lay_reader(sc1.CommandText);
                while (dr1.Read())
                {
                    masach = Convert.ToString(dr1["MASACH"]);
                    if (txtmasach.Text.Equals(masach.Trim(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        sach = true;
                    }
                }
                if (sach)
                {
                    MessageBox.Show("Sách này đã cho mượn", "Thông Báo");
                    btmuon.Enabled = false;
                }
            }
            KetNoi.closeconnect();
        }

        bool taopm = true;
        private void btmuon_Click(object sender, EventArgs e)
        {
            if (ktdocgia && ktsach)
            {
                ngaymuon = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + DateTime.Now.Year.ToString();
                int i = 0;
                Truyxuatdulieu txdl = new Truyxuatdulieu();
                SqlCommand sc = new SqlCommand();
                sc.CommandText = "SELECT *FROM SACH,DOCGIA,PHIEUMUON,SACH_PHIEU WHERE DOCGIA.MADOCGIA='" + txtmadg.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH=SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL";
                SqlDataReader dr = txdl.lay_reader(sc.CommandText);
                while (dr.Read())
                {
                    i++;
                    
                }
                soluongmuon = i;
                muontoida();
                if (soluongmuon == sltoida)
                    MessageBox.Show("Số lượng mượn đã tối đa", "Thông Báo");
                else
                {
                    ArrayList arr = new ArrayList();
                    Truyxuatdulieu txdl1 = new Truyxuatdulieu();
                    SqlCommand sc1 = new SqlCommand();
                    
                    sc1.CommandText = "SELECT *FROM DOCGIA,PHIEUMUON WHERE DOCGIA.MADOCGIA='" + txtmadg.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA";
                    SqlDataReader dr1 = txdl1.lay_reader(sc1.CommandText);
                    while (dr1.Read())
                    {
                        arr.Add(Convert.ToString(dr1["MAPHIEU"]));
                    }
                    for (int j = 0; j < arr.Count; j++)
                        if (Convert.ToString(arr[j]).Trim() == phieutam)
                            taopm = false;                   
                    if (taopm)
                    {
                        laynhanvien();
                        PhieuMuon pm = new PhieuMuon();
                        pm.set_maPM(phieutam);
                        pm.set_madg(txtmadg.Text);
                        pm.set_manv(nv);
                        pm.set_ngaymuon(Convert.ToDateTime(ngaymuon));
                        if (pm.taomoiphieumuon())
                        {      
                        }
                        else
                        {
                            MessageBox.Show("có lỗi không tạo được phiếu mượn", "thông Báo");
                        }
                    }
                    int day = Convert.ToInt16(DateTime.Now.Day.ToString());
                    int month = Convert.ToInt16(DateTime.Now.Month.ToString());
                    int year = Convert.ToInt16(DateTime.Now.Year.ToString());
                    if (month + 3 > 12)
                    {
                        month = month + 3 - 12;
                        year += 1;
                    }
                    else month += 3;
                    ngayhethantra = Convert.ToString(month) + "/" + Convert.ToString(day) + "/" + Convert.ToString(year);
                    PhieuSach ps = new PhieuSach();
                    ps.set_maphieumuon(phieutam);
                    ps.set_masach(txtmasach.Text);
                    ps.set_ngayhethantra(Convert.ToDateTime(ngayhethantra));
                    if (ps.taomoiphieusach())
                    {
                        soluongmuon++;
                        load_listview();
                        MessageBox.Show("Đã cho mượn sách ", "Thông Báo");
                    }
                    else
                        MessageBox.Show("có lỗi ko tạo được phiếu sách", "Thông Báo");
                }
            }
            else
                MessageBox.Show("Phải kiểm tra thông tin trước khi cho mượn", "Thông Báo");
            btmuon.Enabled = false;     
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txthoten.Text = txtdiachi.Text = txtgioitinh.Text = txtDT.Text = txtngaysinh.Text = txtkho.Text = txtngan.Text = txtke.Text = "";
            txttensach.Text = txtnamxb.Text = txtncc.Text = txtgia.Text = txttacgia.Text = txttacgia.Text = txtngonngu.Text = txtnhaxb.Text = "";
            txtlinhvuc.Text = txtlanxuatban.Text = "";
            txtmadg.Text = txtmasach.Text = txtngayhh.Text = "";
            ktsach = false;
            ktdocgia = false;
            btmuon.Enabled = false;
            txtmasach.Enabled = false;
            bttkbandoc.Enabled = true;
            txtmadg.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        public void load_listview()
        {
            int i = 0;
            listView1.Clear();
            listView1.Columns.Add("stt", 50, HorizontalAlignment.Left);
            listView1.Columns.Add("Mã Độc Giả", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("Tên Độc Giả", 180, HorizontalAlignment.Left);
            listView1.Columns.Add("Mã Sách", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("Sách Đã Mượn", 180, HorizontalAlignment.Left);
            listView1.Columns.Add("Ngày Mượn", 180, HorizontalAlignment.Left);
            KetNoi.setconnect();
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT *FROM SACH,DOCGIA,PHIEUMUON,SACH_PHIEU WHERE DOCGIA.MADOCGIA='" + txtmadg.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH=SACH.MASACH and SACH_PHIEU.NGAYTRA IS NULL";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                listView1.Items.Add((i + 1).ToString());
                listView1.Items[i].SubItems.Add(dr["MADOCGIA"].ToString());
                listView1.Items[i].SubItems.Add(dr["HOTEN"].ToString());
                listView1.Items[i].SubItems.Add(dr["MASACH"].ToString());
                listView1.Items[i].SubItems.Add(dr["TENSACH"].ToString());
                listView1.Items[i].SubItems.Add(dr["NGAYMUON"].ToString());
                i++;
            }
        }


    }
}
